package no.uib.hplims.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;

import no.uib.hplims.models.Experiment;
import no.uib.hplims.models.Peptide;
import no.uib.hplims.models.peptideinformation.Freezer;
import no.uib.hplims.models.peptideinformation.PeptideInformation;
import no.uib.hplims.models.peptideinformation.Storage;
import no.uib.hplims.models.peptideinformation.Transition;

public class ExportExperiment {

	private static final String TAB = "\t";

	public File export(Experiment experiment) {
		File f = null;

		try {
			// Create file
			Date dateNow = new Date();
			SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat(
					"MMddyyyy");
			StringBuilder nowMMDDYYYY = new StringBuilder(
					dateformatMMDDYYYY.format(dateNow));

			f = new File(experiment.getName() + " - " + nowMMDDYYYY + ".txt");
			FileWriter fstream = new FileWriter(f);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("Q1\t Q3\t ID\t DP\t EP\t CE\t CXP\n");

			ArrayList<Peptide> peptides = new ArrayList<Peptide>();
			ArrayList<Long> peptideIds = experiment.getPeptideIds();

			Iterator<Long> it = peptideIds.iterator();
			while (it.hasNext()) {
				Peptide p = FacadeFactory.getFacade().find(Peptide.class,
						it.next());
				peptides.add(p);
			}

			Iterator<Peptide> peptIt = peptides.iterator();
			while (peptIt.hasNext()) {
				Peptide p = peptIt.next();
				PeptideInformation pi = FacadeFactory.getFacade().find(
						PeptideInformation.class, p.getPeptideInformationID());
				List<Transition> transitions = pi.getTransitions();
				Iterator<Transition> trIt = transitions.iterator();
				while (trIt.hasNext()) {
					Transition t = trIt.next();
					double q1 = t.getQ1();
					double q3 = t.getQ3();
					String id = generateId(p, t);
					double dp = t.getDeclusteringPotential();
					double ep = t.getExitPotential();
					double ce = t.getCollissionEnergy();
					double cxp = t.getCollissionExitPotential();

					String s = q1 + TAB + q3 + TAB + id + TAB + dp + TAB + ep
							+ TAB + ce + TAB + cxp + "\n";
					out.append(s);
				}

			}
			// Close the output stream
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return f;
	}

	public File exportStorage(Experiment experiment) {
		File f = null;

		try {
			// Create file
			Date dateNow = new Date();
			SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat(
					"MMddyyyy");
			StringBuilder nowMMDDYYYY = new StringBuilder(
					dateformatMMDDYYYY.format(dateNow));

			f = new File(experiment.getName() + " - " + nowMMDDYYYY
					+ "-storage.txt");
			FileWriter fstream = new FileWriter(f);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("Name\t Sequence\t Freezer\t Concentration\t Number of aliquots\n");

			ArrayList<Peptide> peptides = new ArrayList<Peptide>();
			ArrayList<Long> peptideIds = experiment.getPeptideIds();

			Iterator<Long> it = peptideIds.iterator();
			while (it.hasNext()) {
				Peptide p = FacadeFactory.getFacade().find(Peptide.class,
						it.next());
				peptides.add(p);
			}

			Iterator<Peptide> peptIt = peptides.iterator();
			while (peptIt.hasNext()) {
				Peptide p = peptIt.next();
				PeptideInformation pi = FacadeFactory.getFacade().find(
						PeptideInformation.class, p.getPeptideInformationID());
				List<Storage> storage = pi.getStorageInfo();
				Iterator<Storage> storageIt = storage.iterator();
				while (storageIt.hasNext()) {
					Storage s = storageIt.next();
					String name = p.getPeptideName();
					String sequence = p.getPeptideSequence();
					Freezer freezer = s.getFreezer();
					String concentration = s.getConcentration();
					int numAliquots = s.getNumberOfAliquots();

					String r = name + TAB + sequence + TAB + freezer + TAB
							+ concentration + TAB + numAliquots + "\n";
					out.append(r);
				}

			}
			// Close the output stream
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return f;
	}

	private String generateId(Peptide p, Transition t) {
		String id = p.getBelongsToOrder().getSalesOrderCode() + "_"
				+ p.getWellID() + "_" + p.getPeptideName() + "."
				+ p.getPeptideSequence() + "." + t.getIon()
				+ t.getFragmentIon();
		return id;
	}
}
